package com.face.controller;

import com.face.common.Page;
import com.face.common.Result;
import com.face.common.ResultCode;
import com.face.entity.Visit;
import com.face.service.VisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/visit")
public class VisitController {

    @Autowired
    private VisitService visitService;

    @GetMapping("/count")
    public Result countList(String shopId, @RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "limit", defaultValue = "10") Integer limit,
                            @RequestParam(name = "start", required = false) String start, @RequestParam(name = "end", required = false) String end){
        if(shopId==null||shopId.isEmpty()){
            return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
        }
        Page<Map<String, Object>> result = visitService.getVisitCountList(shopId, page, limit, start, end);
        return Result.success(result);
    }

    @GetMapping("/{userId}")
    public Result findByUserId(@PathVariable String userId, String shopId, @RequestParam(name = "page", defaultValue = "1") Integer page,
                               @RequestParam(name = "limit", defaultValue = "10") Integer limit,
                               @RequestParam(name = "start", required = false) String start, @RequestParam(name = "end", required = false) String end) {
        if(shopId==null||shopId.isEmpty()){
            return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
        }
        Page<Visit> result = visitService.findByUserId(shopId, userId, page, limit, start, end);
        return Result.success(result);
    }
}
